﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class NoticeForm : Form
    {
        public static List<NoticeForm> list = new List<NoticeForm>();   // 用于存储提示信息队列
        

        //对提示信息队列中的所有提示信息，重新布局定位
        public static void reLocation()
        {
            foreach (NoticeForm notie in list)
                notie.autoLocation();
        }

        public string id;       // 信息id
        public TYPE type;       // 信息类型

        public string info = null;      // 提示信息
        public string details = null;   // 详细说明信息

        public NoticeForm()
        {
            InitializeComponent();
        }

        // 创建一个信息提示窗体，给定id,类型和显示的信息, details为附加的详情信息
        public NoticeForm(String id, TYPE type, string info, string status, string details = null)
        {
            InitializeComponent();
            setInfo(id, type, info, status);

            this.info = info;
            if (details != null) this.details = details;
        }

        // 检查提示信息的id，不同则提示
        public void TipId_CheckShow()
        {
            if (type == TYPE.Tip)
            {
                String value = Tool.RegistryStrValue(@"Scimence\TaskManager\Set\TipInfos", info);
                if (!value.Equals(id))
                {
                    Tool.RegistrySave(@"Scimence\TaskManager\Set\TipInfos", info, id);    // 记录对应提示信息的id
                    this.Show();
                }
            }
        }


        // 创建一个信息提示窗体，给定id,类型和显示的信息
        public NoticeForm(String id, TYPE type, string info)
        {
            InitializeComponent();
            setInfo(id, type, info);
        }

        // 设置消息提示内容
        public void setInfo(String id, TYPE type, string info, string status)
        {
            this.id = id;
            this.type = type;
            transLabel.LabelText = TypeStr(type) + ":" + info;
            if (!status.Equals("")) transLabel_status.LabelText = FinishStatus(status);
        }

        public string FinishStatus(string status)
        {
            if (status.Equals("done") || status.Equals("developed") || status.Equals("tested") || status.Equals("resolved") || status.Equals("0")) return "完成";
            else if (status.Equals("（将到期）")) return "到期";
            else return "NEW";
        }

        // 设置消息提示内容
        public void setInfo(String id, TYPE type, string info)
        {
            this.id = id;
            this.type = type;
            transLabel.LabelText = TypeStr(type) + ":" + info;
        }

        private string TypeStr(TYPE type)
        {
            if (type == TYPE.Task) return "任务";
            else if (type == TYPE.Demand) return "需求";
            else if (type == TYPE.Bug) return "BUG";
            else if (type == TYPE.Testtask) return "测试";
            else if (type == TYPE.Daily) return "日记";
            else if (type == TYPE.Remind) return "消息";
            else if (type == TYPE.Food) return "点餐提醒";
            else if (type == TYPE.Tip) return "提醒";
            else return "其它";
        }

        
        private void NoticeForm_Load(object sender, EventArgs e)
        {
            this.TransparencyKey = Color.White;             //设置默认透明色  
            this.BackColor = this.TransparencyKey;          //设置当前窗体的背景色为透明  
            this.FormBorderStyle = FormBorderStyle.None;    //隐藏窗体边框 

            this.TopMost = true;                            //设为顶层显示
            this.ShowInTaskbar = false;                     //不在任务栏中显示

            autoLocation(); // 自动对信息进行布局
        }

        // 根据提示信息在队列中的位置，在桌面上自动定位
        public void autoLocation()
        {
            // 获取当前提示在队列中的索引位置
            int index = 0;
            if (list.Contains(this)) index = list.IndexOf(this);
            else
            {
                list.Add(this);
                index = list.IndexOf(this);
            }
            index++;

            // 设置窗体的坐标
            Left = SystemInformation.WorkingArea.Width - pictureBox_background.Width;
            Top  = SystemInformation.WorkingArea.Height - pictureBox_background.Height*index;
        }

        // 关闭当前提示信息窗体
        private void transPictureBox_close_Click(object sender, EventArgs e)
        {
            this.Close();
            if(list.Contains(this)) list.Remove(this);
            reLocation();                   // 对其余提示信息重新布局

            loginForm.saveIdData(type, id); // 记录已经提示过的信息id
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.TopMost = true;    //保持提示信息处于最顶端，定时3秒后修改一次
            timer1.Enabled = false; //仅置顶一次
        }

        // 双击时，跳转至对应消息的详情
        private void Notice_DoubleClick(object sender, EventArgs e)
        {
            if (id != null)
            {
                if (this.type == TYPE.Tip) FloatMenu.openTipDetail(info, details, "乐堂禅道客户端", "", TypeStr(type));
                else
                {
                    bool inForm = ((Control.ModifierKeys & Keys.Control) == Keys.Control); // CTRL is pressed
                    FloatMenu.openDetail(this.type, this.id, inForm);
                }

                transPictureBox_close_Click(null, null);    // 双击查看信息后自动关闭
            }
        }
    }
}
